<template>
    <div class="app-container" v-loading.fullscreen.lock="fullLoading">
        <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" label-width="68px">
            <el-form-item label="学号" prop="stuNo">
                <el-input v-model="queryParams.stuNo" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="学生姓名" prop="stuName">
                <el-input v-model="queryParams.stuName" placeholder="请输入学生姓名" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="奖项" prop="code">
                <el-select v-model="queryParams.code">
                    <el-option v-for="(v, i) in code_list" :key="i" clearable :value="v.code"
                        :label="v.name"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="审核状态" prop="status">
                <el-select size="mini" v-model="queryParams.status" placeholder="审核状态" clearable>
                    <el-option v-for="(v, i) in applyStatus" :key="i" :label="v.label" :value="v.value" />
                </el-select>
            </el-form-item>
            <el-form-item label=" ">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                <el-button icon="el-icon-download" type="success" size="mini" @click="exportVClick">导出审核信息</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="table_data">
            <el-table-column label="奖项" prop="typeName" align="center" />
            <el-table-column label="学生" prop="stuName" align="center" />
            <el-table-column label="学号" prop="stuNo" align="center" />
            <el-table-column label="提交时间" prop="createTime" align="center" />
            <el-table-column label="申请状态" align="center">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.audius_detail_type" :value="scope.row.applyStatus" />
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="mini" icon="el-icon-view"
                        @click="auditVClick(scope.row)">查看详情</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <el-dialog title="申请" :visible.sync="auditV" fullscreen :close-on-click-modal="false">
            <el-form class="detail" label-width="160px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="班级">
                            <el-input readonly size="mini" v-model="auditForm.className"
                                class="auditForm-input"></el-input>
                        </el-form-item>
                        <el-form-item label="姓名">
                            <el-input readonly size="mini" v-model="auditForm.stuName"
                                class="auditForm-input"></el-input>
                        </el-form-item>
                        <el-form-item label="奖项">
                            <el-input readonly size="mini" v-model="auditForm.typeName"
                                class="auditForm-input"></el-input>
                        </el-form-item>
                        <el-form-item label="政治面貌">
                            <el-input readonly size="mini" class="auditForm-input" v-model="auditForm.zzmm"></el-input>
                        </el-form-item>
                        <el-form-item label="职务">
                            <el-input readonly size="mini" class="auditForm-input"
                                v-model="auditForm.classPost"></el-input>
                        </el-form-item>
                        <el-form-item v-if="!isEmpty(auditForm.classtwoSure)" label="第二课堂学分认定">
                            <el-input readonly size="mini" class="auditForm-input"
                                v-model="auditForm.classtwoSure"></el-input>
                        </el-form-item>
                        <!-- <el-form-item v-if="!isEmpty(auditForm.youthPercent)" label="青年大学习学习率">
                            <el-input readonly size="mini" class="auditForm-input"
                                v-model="auditForm.youthPercent"></el-input>
                        </el-form-item> -->
                        <el-form-item v-if="!isEmpty(auditForm.bankCard)" label="中国农业银行银行卡号">
                            <el-input readonly size="mini" class="auditForm-input"
                                v-model="auditForm.bankCard"></el-input>
                        </el-form-item>
                        <el-form-item v-if="!isEmpty(auditForm.bankAddr)" label="开户行">
                            <el-input readonly size="mini" class="auditForm-input"
                                v-model="auditForm.bankAddr"></el-input>
                        </el-form-item>
                        <el-form-item label="获奖情况">
                            <el-input readonly type="textarea" :maxlength="1000"
                                :autosize="{ minRows: 10, maxRows: 10 }" class="auditForm-input"
                                v-model="auditForm.goodHis"></el-input>
                        </el-form-item>
                        <el-form-item label="主要事迹">
                            <el-input readonly type="textarea" :maxlength="1000"
                                :autosize="{ minRows: 10, maxRows: 10 }" class="auditForm-input"
                                v-model="auditForm.mainHis"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="查看">
                            <el-button size="mini" @click="myScoreVClick">查看学年成绩</el-button>
                            <el-button size="mini" @click="iamVClick">查看学年思想品德</el-button>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item v-if="!isEmpty(auditForm.fdyComment)" label="辅导员审核意见">
                            <el-input readonly type="textarea" :maxlength="1000"
                                :autosize="{ minRows: 10, maxRows: 10 }" class="auditForm-input"
                                v-model="auditForm.fdyComment"></el-input>
                        </el-form-item>
                        <el-form-item v-if="!isEmpty(auditForm.deptComment)" label="学院审核意见">
                            <el-input readonly type="textarea" :maxlength="1000"
                                :autosize="{ minRows: 10, maxRows: 10 }" class="auditForm-input"
                                v-model="auditForm.deptComment"></el-input>
                        </el-form-item>
                        <el-form-item v-if="!isEmpty(auditForm.finalComment)" label="学工审核意见">
                            <el-input readonly type="textarea" :maxlength="1000"
                                :autosize="{ minRows: 10, maxRows: 10 }" class="auditForm-input"
                                v-model="auditForm.finalComment"></el-input>
                        </el-form-item>
                    </el-col>

                </el-row>

            </el-form>

            <el-dialog :visible.sync="iamV" title="学生思想品德" append-to-body :close-on-click-modal="false">
                <el-table :data="iam_count">
                    <el-table-column label="学年" prop="stuYearName" align="center" />
                    <el-table-column label="总记录数" prop="iamCount" align="center" />
                    <el-table-column label="加分数" prop="addCount" align="center" />
                    <el-table-column label="扣分数" prop="minusCount" align="center" />
                </el-table>
            </el-dialog>

            <el-dialog :visible.sync="myScoreV" title="学生成绩" append-to-body :close-on-click-modal="false">
                <el-table :data="score_list">
                    <el-table-column label="学年" prop="stuYearName" align="center" />
                    <el-table-column label="平均学分绩" prop="stuScore" align="center" />
                    <el-table-column label="综合素质总成绩" prop="cphScore" align="center" />
                    <el-table-column label="体能测试成绩" prop="sportScore" align="center" />
                    <el-table-column label="平均学分绩班级排名" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.stuClassRank + '/' + scope.row.classCount }}
                        </template>
                    </el-table-column>
                    <el-table-column label="综合素质总成绩班级排名" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.cphClassRank + '/' + scope.row.classCount }}
                        </template>
                    </el-table-column>
                    <el-table-column label="平均学分绩班级排名百分比" align="center">
                        <template slot-scope="scope">
                            {{ Math.round(scope.row.stuClassRank / scope.row.classCount * 10000) / 100 + "%" }}
                        </template>
                    </el-table-column>
                    <el-table-column label="学年综合素质测评班级排名百分比" align="center">
                        <template slot-scope="scope">
                            {{ Math.round(scope.row.cphClassRank / scope.row.classCount * 10000) / 100 + "%" }}
                        </template>
                    </el-table-column>
                </el-table>
                <el-table :data="count_list">
                    <el-table-column label="学年" prop="stuYearName" align="center" />
                    <el-table-column label="课程总门数" prop="courseCount" align="center" />
                    <el-table-column label="通过门数" prop="passCount" align="center" />
                    <el-table-column label="未通过门数" prop="unpassCount" align="center" />
                </el-table>


                <el-table :data="course_list" v-loading="course_loading">
                    <el-table-column label="学期" prop="termName" align="center" />
                    <el-table-column label="课程" prop="courseName" align="center" />
                    <el-table-column label="成绩" prop="courseScore" align="center" />
                    <el-table-column label="是否通过" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.isPass == "true" ? "通过" : "未通过" }}
                        </template>
                    </el-table-column>
                </el-table>
                <pagination v-show="courseTotal > 0" :total="courseTotal" :page.sync="courseParams.pageNum"
                    :limit.sync="courseParams.pageSize" @pagination="listStuYearCourseScore" />
            </el-dialog>

        </el-dialog>



        <el-dialog title="导出奖项审核结果" :visible.sync="exportV" width="500px">

            <el-select placeholder="请选择学年" v-model="exportForm.stuYearId">
                <el-option v-for="(v, i) in year_list" :key="i" clearable :value="v.id"
                    :label="v.stuYearName"></el-option>
            </el-select>

            <el-select placeholder="请选择奖项" v-model="exportForm.typeName">
                <el-option v-for="(v, i) in code_list" :key="i" clearable :value="v.code" :label="v.name"></el-option>
            </el-select>

            <el-select style="width: 442px;" placeholder="请选择专业" v-model="exportForm.majorId">
                <el-option v-for="(v, i) in major_list" :key="i" clearable :value="v.majorId"
                    :label="v.majorName"></el-option>
            </el-select>
            <div style="font-size: small;margin: 10px 0 10px 10px; color: red;">*学年、奖项是必选项，专业不选就默认导出全部。</div>
            <el-button @click="doExport">导出</el-button>
        </el-dialog>


    </div>
</template>
<script>
import {
    listDeptAudit, lookDetail, listStuScoreClassRank, listStuYearCourseScore, countStuPass, countStuIam, deptAudit
    , xwExport, applyStatus
} from "@/api/stuCQS/good/audit";
import { listEnableYear as listYear } from "@/api/stuCQS/basedata/year";
import { listOwnMajor } from "@/api/stuCQS/info-fill/cqScore";


import * as XLSX from 'xlsx';
import { workbook2blob, openDownloadDialog, file2Xce } from "@/api/helpFunc";
import lodash from "lodash";
import { isEmpty } from "@/api/helpFunc";

export default {
    name: "xw-audit",
    dicts: ['audius_detail_type'],
    data() {
        return {
            applyStatus,
            code_list: [
                {
                    name: "学业奖学金",
                    code: "XYJXJ"
                },
                {
                    name: "三好学生",
                    code: "SHXS"
                },
                {
                    name: "优秀学生干部",
                    code: "YXXSGB"
                }
            ],

            isEmpty,

            fullLoading: false,

            loading: false,
            table_data: [],
            total: 0,
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                code: null,
                status: null,
                stuNo: "",
                stuName: ""
            },

            auditV: false,
            auditForm: {},
            material_list: [],

            score_list: [],
            myScoreV: false,
            course_list: [],
            courseParams: {
                pageNum: 1,
                pageSize: 10
            },
            courseTotal: 0,
            course_loading: false,

            count_list: [],

            iamV: false,
            iam_count: [],

            doForm: {
                auditId: null,
                status: null,
                common: ""
            },

            exportForm: {
                typeName: null,
                majorId: null,
                stuYearId: null
            },
            exportV: false,

            year_list: [],
            major_list: []

        }
    },
    created() {
        this.listOwnMajor();
        this.listAllStuYear();
        this.getList();
    },
    methods: {
        async doExport() {
            let sdata = { ...this.exportForm };
            if (isEmpty(sdata.stuYearId)) {
                this.$message.info("请选择学年");
                return;
            }
            if (isEmpty(sdata.typeName)) {
                this.$message.info("请选择奖项");
                return;
            }

            this.loading = true;
            let res = await xwExport(sdata);
            this.loading = false;
            if (res.code == 200) {
                let data = res.data;
                let fileName = "";
                let fileHeader = [];
                let fileHeaderZh = {};
                let fileCol = [];
                switch (sdata.typeName) {
                    case "XYJXJ":
                        fileHeader = ['stuYearName', 'deptName', 'majorName', 'className', 'stuNo', 'stuName', 'typeName', 'applyStatus', 'bankCard', 'bankAddr', 'importTime', 'teacherName'];
                        fileHeaderZh = {
                            'stuYearName': "学年",
                            "deptName": "学院",
                            "majorName": "专业",
                            "className": "班级",
                            "stuNo": "学号",
                            "stuName": "姓名",
                            "typeName": "奖项",
                            "applyStatus": "审核状态",
                            "bankCard": "银行卡号",
                            "bankAddr": "开户行",
                            "importTime": "导出时间",
                            'teacherName': "辅导员"
                        };
                        fileCol = [
                            { wch: 15 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 10 },
                            { wch: 10 },
                            { wch: 15 },
                            { wch: 20 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 40 }
                        ];
                        fileName = "学业奖学金名单";
                        break;
                    case "SHXS":
                        fileHeader = ['stuYearName', 'deptName', 'majorName', 'className', 'stuNo', 'stuName', 'typeName', 'applyStatus', 'importTime', 'teacherName'];
                        fileHeaderZh = {
                            'stuYearName': "学年",
                            "deptName": "学院",
                            "majorName": "专业",
                            "className": "班级",
                            "stuNo": "学号",
                            "stuName": "姓名",
                            "typeName": "奖项",
                            "applyStatus": "审核状态",
                            "importTime": "导出时间",
                            'teacherName': "辅导员"
                        };
                        fileCol = [
                            { wch: 15 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 10 },
                            { wch: 10 },
                            { wch: 15 },
                            { wch: 15 },
                            { wch: 40 },
                            { wch: 40 }
                        ];
                        fileName = "三好学生名单";
                        break;
                    case "YXXSGB":
                        fileHeader = ['stuYearName', 'deptName', 'majorName', 'className', 'stuNo', 'stuName', 'typeName', 'applyStatus', 'importTime', 'teacherName'];
                        fileHeaderZh = {
                            'stuYearName': "学年",
                            "deptName": "学院",
                            "majorName": "专业",
                            "className": "班级",
                            "stuNo": "学号",
                            "stuName": "姓名",
                            "typeName": "奖项",
                            "applyStatus": "审核状态",
                            "importTime": "导出时间",
                            'teacherName': "辅导员"
                        };
                        fileCol = [
                            { wch: 15 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 40 },
                            { wch: 10 },
                            { wch: 10 },
                            { wch: 15 },
                            { wch: 15 },
                            { wch: 40 },
                            { wch: 40 }
                        ];
                        fileName = "优秀学生干部名单";
                        break;
                }

                const arrayWithHeader = [fileHeaderZh, ...data];

                var sheet1 = XLSX.utils.json_to_sheet(arrayWithHeader,
                    {
                        header: fileHeader,
                        skipHeader: true
                    });
                sheet1["!cols"] = fileCol;

                // 创建一个新的空的workbook
                var wb = XLSX.utils.book_new();
                // 为每一个工作簿设置名称并添加到workbook（excel表）中
                XLSX.utils.book_append_sheet(wb, sheet1, fileName);
                const workbookBlob = workbook2blob(wb);  // 辅助函数workbook2blob
                // 下载文档并添加文件名称
                openDownloadDialog(workbookBlob, fileName + ".xlsx");  // 辅助函数openDownloadDialog


            } else {
                this.$message.info("出错啦，刷新试试");
            }
        },
        async exportVClick() {
            this.exportV = true;
        },
        async listAllStuYear() {
            let res = await listYear();
            if (res.code == 200) {
                this.year_list = [...res.rows];
            }
        },
        async listOwnMajor() {
            let res = await listOwnMajor();
            if (res.code == 200) {
                this.major_list = [...res.data];
            }
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        async getList() {
            this.loading = true;
            let res = await listDeptAudit(this.queryParams);
            this.loading = false;
            if (res.code == 200) {
                this.table_data = [...res.rows];
                this.total = res.total;
            }
        },
        async auditVClick(v) {
            let id = v.applyId;
            this.fullLoading = true;
            let res = await lookDetail(id);
            this.fullLoading = false;
            if (res.code == 200) {
                if (!isEmpty(res.data.material)) {
                    this.material_list = res.data.material.split(",");
                } else {
                    this.material_list = [];
                }
                this.auditForm = res.data;
                this.auditV = true;
            }
        },
        async countStuIam() {
            let stuNo = this.auditForm.stuNo;
            let res = await countStuIam(stuNo);
            if (res.code == 200) {
                this.iam_count = res.data;
            }
        },

        async countStuPass() {
            let stuNo = this.auditForm.stuNo;

            let res = await countStuPass(stuNo);
            if (res.code == 200) {
                this.count_list = [...res.data];
            }
        },
        async listStuYearCourseScore() {
            let stuNo = this.auditForm.stuNo;

            this.course_loading = true;
            let res = await listStuYearCourseScore(stuNo, this.courseParams);
            this.course_loading = false;

            if (res.code == 200) {
                this.course_list = [...res.rows];
                this.courseTotal = res.total;
            }
        },
        async listStuScoreClassRank() {
            let stuNo = this.auditForm.stuNo;
            let res = await listStuScoreClassRank(stuNo);
            if (res.code == 200) {
                this.score_list = lodash.cloneDeep(res.data);
            }
        },
        iamVClick() {
            this.countStuIam();
            this.iamV = true;
        },
        myScoreVClick() {
            this.countStuPass();
            this.listStuScoreClassRank();
            this.listStuYearCourseScore();
            this.myScoreV = true;
        },
        async doAudit() {
            let sdata = { ...this.doForm };
            if (isEmpty(sdata.status)) {
                this.$message.info("请选择是否通过");
                return;
            }
            if (isEmpty(sdata.common)) {
                this.$message.info("请填写审核意见");
                return;
            }
            sdata.auditId = parseInt(this.auditForm.auditId);
            this.fullLoading = true;
            let res = await deptAudit(sdata);
            this.fullLoading = false;
            if (res.code == 200) {
                this.getList();
                this.auditForm = {};
                this.doForm = {
                    auditId: null,
                    status: null,
                    common: ""
                };
                this.auditV = false;
                this.$message.success(res.msg);
            }
        }
    }
}

</script>
<style lang="scss" scoped>
.addForm-input {
    width: 400px;
}

.detail {
    .el-form-item {
        margin-bottom: 0px;
    }
}
</style>